/* Copyright - Apache License 2.0
 * 
 * The project "kyou" is
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kyou.pack.segment.param;

import java.nio.charset.Charset;

import net.kyou.pack.PackContext;
import net.kyou.pack.segment.Segment;

/**
 * 双元参数 这种类型的参数接受并处理另外一个参数的计算结果
 * 
 * @author NuclearG (<a href="mailto:nuclearg@163.com">nuclearg@163.com</a>)
 */
abstract class BinaryParam extends Param {
    private final Param arg;

    BinaryParam(String expr, Segment segment, Param arg, Charset encoding) {
        super(expr, segment, encoding);

        this.arg = arg;

        ParamType expectType = this.getClass().getAnnotation(ParamTag.class).require();
        Param.__checkType(arg, expectType);
    }

    @Override
    public Object eval(PackContext context) {
        Object result = this.arg.eval(context);
        return this.eval(result, context);
    }

    /**
     * 进一步处理前一个参数的计算结果
     * 
     * @param result
     *            前一个参数的计算结果
     * @param context
     *            组包上下文
     * @return 当前参数的计算结果
     */
    abstract Object eval(Object result, PackContext context);
}
